package com.dylon.system.service.impl;

import com.dylon.common.constant.UserConstants;
import com.dylon.common.exception.BusinessException;
import com.dylon.common.utils.Convert;
import com.dylon.common.utils.StringUtils;
import com.dylon.system.domain.SysProject;
import com.dylon.system.mapper.SysProjectMapper;
import com.dylon.system.service.ISysProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysProjectServiceImpl implements ISysProjectService {

    @Autowired
    private SysProjectMapper projectMapper;

    /**
     * 查询项目数据
     * @param project 项目信息
     * @return 项目信息集合
     */
    @Override
    public List<SysProject> selectProjectList(SysProject project){
        return projectMapper.selectProjectList(project);
    }

    /**
     * 通过项目ID查询项目
     * @param projectId 项目ID
     * @return 项目对象信息
     */
    @Override
    public SysProject selectProjectById(Long projectId){
        return projectMapper.selectProjectById(projectId);
    }

    /**
     * 校验项目名称是否唯一
     * @param project 项目信息
     * @return 结果
     */
    @Override
    public String checkProjectNameUnique(SysProject project){
        Long projectId = StringUtils.isNull(project.getProjectId()) ? -1L : project.getProjectId();
        SysProject info = projectMapper.checkProjectNameUnique(project.getProjectName());
        if (StringUtils.isNotNull(info) && info.getProjectId().longValue() != projectId.longValue()) {
            return UserConstants.PROJECT_NAME_NOT_UNIQUE;
        }
        return UserConstants.PROJECT_NAME_UNIQUE;
    }

    /**
     * 通过项目ID删除项目
     * @param projectId 项目ID
     * @return 结果
     */
    @Override
    public boolean deleteProjectById(Long projectId) throws Exception{
        SysProject project = selectProjectById(projectId);
        if (countProjectUsedById(projectId) > 0) {
            throw new BusinessException(String.format("%1$s下已存在模块,不能删除", project.getProjectName()));
        }
        return projectMapper.deleteProjectById(projectId) > 0 ? true : false;
    }

    /**
     * 批量删除项目信息
     * @param ids 需要删除的数据ID
     * @return 结果
     * @throws Exception 异常
     */
    @Override
    public int deleteProjectByIds(String ids) throws Exception{
        Long[] projectIds = Convert.toLongArray(ids);
        for (Long projectId : projectIds) {
            SysProject project = selectProjectById(projectId);
            if (countProjectUsedById(projectId) > 0) {
                throw new BusinessException(String.format("%1$s下已存在模块,不能删除", project.getProjectName()));
            }
        }
        return projectMapper.deleteProjectByIds(projectIds);
    }

    /**
     * 修改保存项目信息
     * @param project 项目信息
     * @return 结果
     */
    @Override
    public int updateProject(SysProject project){
        return projectMapper.updateProject(project);
    }

    /**
     * 新增保存项目信息
     * @param project 项目信息
     * @return 结果
     */
    @Override
    public int insertProject(SysProject project){
        return projectMapper.insertProject(project);
    }

    /**
     * 项目状态修改
     * @param project 项目信息
     * @return 结果
     */
    @Override
    public int changeStatus(SysProject project){
        return projectMapper.updateProject(project);
    }

    /**
     * 查询项目使用数量
     * @param projectId 角色ID
     * @return 结果
     */
    @Override
    public int countProjectUsedById(Long projectId){
        return projectMapper.countProjectUsedById(projectId);
    }
}
